package com.symaster.backups.service.impl;

import com.symaster.backups.enumerate.BackupFolderPathType;
import com.symaster.backups.enumerate.SystemConfig;
import com.symaster.backups.model.FileSystemMonitorBackupItemModel;
import com.symaster.backups.model.FileSystemMonitorModel;
import com.symaster.backups.model.SystemConfigModel;
import com.symaster.backups.repository.FileSystemMonitorBackupItemRepository;
import com.symaster.backups.repository.FileSystemMonitorRepository;
import com.symaster.backups.repository.SystemConfigRepository;
import com.symaster.backups.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yinmiao
 * @version 2024/2/19
 */
@Service
public class SystemConfigServiceImpl implements SystemConfigService {

    private final SystemConfigRepository systemConfigRepository;
    private final FileSystemMonitorRepository fileSystemMonitorRepository;
    private final FileSystemMonitorBackupItemRepository fileSystemMonitorBackupItemRepository;

    @Autowired
    public SystemConfigServiceImpl(SystemConfigRepository systemConfigRepository,
                                   FileSystemMonitorRepository fileSystemMonitorRepository,
                                   FileSystemMonitorBackupItemRepository fileSystemMonitorBackupItemRepository) {
        this.systemConfigRepository = systemConfigRepository;
        this.fileSystemMonitorRepository = fileSystemMonitorRepository;
        this.fileSystemMonitorBackupItemRepository = fileSystemMonitorBackupItemRepository;
    }

    @Override
    public String backupFolderRoot() {
        SystemConfigModel systemConfigModel = systemConfigRepository
                .findByKey(SystemConfig.BACKUP_FOLDER_PATH_TYPE.getKey())
                .orElse(null);

        if (systemConfigModel == null || BackupFolderPathType.ofKey(systemConfigModel.getStringValue()) == BackupFolderPathType.APPLICATION_ROOT) {
            return "backup";
        } else {
            SystemConfigModel systemConfigModel1 = systemConfigRepository
                    .findByKey(SystemConfig.BACKUP_FOLDER.getKey())
                    .orElseThrow(NullPointerException::new);
            return systemConfigModel1.getStringValue();
        }
    }

    @Override
    public String getSrcPath(FileSystemMonitorBackupItemModel model) {

        FileSystemMonitorModel monitorModel = fileSystemMonitorRepository
                .findById(model.getFileSystemMonitorId())
                .orElseThrow(NullPointerException::new);

        File file = new File(monitorModel.getPathName());
        if (file.isFile()) {
            return file.toPath().toString();
        }

        List<String> pathNames = new ArrayList<>();
        montage(model, pathNames);

        StringBuilder string = new StringBuilder(file.toPath().toString());

        for (int i = pathNames.size() - 1; i >= 0; i--) {
            string.append(File.separator).append(pathNames.get(i));
        }

        return string.toString();
    }

    private void montage(FileSystemMonitorBackupItemModel model, List<String> pathNames) {

        pathNames.add(model.getPathName());

        if (model.getPatent() != null) {
            FileSystemMonitorBackupItemModel fileSystemMonitorBackupItemModel = fileSystemMonitorBackupItemRepository.findById(model.getPatent()).orElseThrow(NullPointerException::new);
            montage(fileSystemMonitorBackupItemModel, pathNames);
        }
    }
}
